$(function(){
    var weekdayArr = [lang('weekComplete.7'),lang('weekComplete.1'),lang('weekComplete.2'),lang('weekComplete.3'),lang('weekComplete.4'),lang('weekComplete.5'),lang('weekComplete.6')];
    $('#startTime').fdatepicker({
        format: 'yyyy-mm-dd',
        startDate: "2000-01-01",
        endDate: '2060-01-01',
    }); 
    $('#endTime').fdatepicker({
        format: 'yyyy-mm-dd',
        startDate: "2000-01-01",
        endDate: '2060-01-01',
    }); 
    $('#startTimeEdit').fdatepicker({
        format: 'yyyy-mm-dd',
        startDate: "2000-01-01",
        endDate: '2060-01-01',
    }); 
    $('#endTimeEdit').fdatepicker({
        format: 'yyyy-mm-dd',
        startDate: "2000-01-01",
        endDate: '2060-01-01',
    });     
    $('#startTime').val($('#doc').data('today')); 
    $('#endTime').val($('#doc').data('end'));    
    //优惠券列表
    var getHotelCoupon = function(){
        ajaxRequest('/Home/BookSetting/hasCoupon', {
            'hotel':window.currentHotel
        }, 'post', function (data) {
            var info = data.data;
            var couponListHTML = '';
            var templateCouponList = $('#templateCouponList');
            $.each(info, function (index, info) {
                var template = $(templateCouponList.clone().html());
                template.data('info',info);
                template.attr('data-couponid',info.couponId);
                template.find('.used-coupon').text(info.couponUsed);
                template.find('.coupon-requirement').text(lang('Coupon.18')+window.symbols+info.couponPrice +lang('Coupon.19'));
                template.find('.start-time').text(info.couponStart);
                template.find('.end-time').text(info.couponEnd);
                template.find('.coupon-desc').text(info.couponName);
                template.find('.coupon-edit').attr('data-couponid',info.couponId);
                if(parseInt(info.couponType) == 1){
                    template.find('.figure-coupon').removeClass('hidden');
                    template.find('.figure-coupon span').text(info.couponMinusPrice);
                    template.find('.type-coupon').text(lang('Coupon.01'));
                }else{
                    template.find('.discount-coupon').removeClass('hidden');
                    template.find('.discount-coupon span').text(info.couponDiscount);
                    template.find('.type-coupon').text(lang('Coupon.02'));
                }
                var couponNumber = info.couponNumber;
                if(couponNumber == 0){
                    couponNumber = lang('Coupon.03');
                }
                template.find('.unused-coupon').text(couponNumber);
                var weekDay = '';
                $.each(info.couponWeek,function(item,value){
                    weekDay+= '、'+weekdayArr[value];
                });
                var dateDay = '';
                $.each(info.couponDate,function(item,value){
                    dateDay+= '、'+value.date;
                });
                if(weekDay.substring(1)!=''){
                    weekDay = lang('Coupon.04') + weekDay.substring(1);
                    if(dateDay.substring(1)!=''){
                        dateDay = '、' + dateDay.substring(1);
                    }            
                }else{
                    if(dateDay.substring(1)!=''){
                        dateDay = lang('Coupon.04') + dateDay.substring(1);
                    }            
                }
                if(info.isAllRoomType){
                    var addRoomType = lang('Coupon.05')+weekDay+dateDay;
                }else{
                    var addRoomType = lang('Coupon.06')+weekDay+dateDay;
                }
                if(info.isOutDate){
                    template.addClass('overdue');
                }
                template.find('.not-room').text(addRoomType);
                couponListHTML += template.prop('outerHTML');
            });
            $('#couponListContainer').html(couponListHTML);           
        }, function (error) {          
            if (error.responseJSON !== undefined) {
                modalAlert(error.responseJSON.info);
            }
        });           
    }
    getHotelCoupon();
    //删除优惠券
    $('#couponListContainer').on('click','.coupon-delete',function(e){
        e.stopPropagation();
        var couponId = $(this).closest('.couponList').data('couponid');
        ajaxRequest('/Home/BookSetting/deleteCoupon', {
            'hotel':window.currentHotel,
            'couponId':couponId
        }, 'post', function (data) {
            getHotelCoupon();
        }, function (error) {          
            if (error.responseJSON !== undefined) {
                modalAlert(error.responseJSON.info);
            }
        });   
    });
    
    //领取记录
    $('#couponListContainer').on('click','.couponList',function(e){
        var couponId = $(this).data('couponid');
        ajaxRequest('/Home/BookSetting/getHotelUserCoupons', {
            'hotel':window.currentHotel,
            'couponId':couponId
        }, 'post', function (data) {
            var recordInfo = data.data;
            if (recordInfo.length === 0) {
                $('#recordListBody').html('').addClass('hidden');
                $('#recordListBodyEmpty').removeClass('hidden');
            } else {
                $('#recordListBodyEmpty').addClass('hidden');
                var recordHTML = '';
                $.each(recordInfo, function (key, value) {
                    var templateHTML = $($('#templateRecord').clone().html());
                    templateHTML.find('.record-num').html(parseInt(key)+1);
                    templateHTML.find('.record-name').html(value.name);
                    templateHTML.find('.record-mobile').html(value.mobile);
                    templateHTML.find('.record-time').html(value.getDate);
                    templateHTML.find('.record-status').html(value.userCouponType);
                    templateHTML.find('.record-order').html(value.orderNumber);
                    templateHTML.find('.record-overdue').html(value.outDate);
                    recordHTML += templateHTML.prop('outerHTML');
                });
                $('#recordListBody').removeClass('hidden').html(recordHTML);
            }   
        }, function (error) {          
            if (error.responseJSON !== undefined) {
                modalAlert(error.responseJSON.info);
            }
        });   
    });    
    
    //获取优惠券详情
    var formGroup = $('#editCouponModal .form-group');
    $('#couponListContainer').on('click','.coupon-edit', function(e) {
        e.stopPropagation();
        var couponId = $(this).data('couponid');
        var editCouponModal = $('#editCouponModal');
        var crowdList = $('#editCouponModal .use-crowd .list');
        editCouponModal.modal('show');
        crowdList.find('.checkbox-btn').removeClass('active');
        $('#calendar-panel .week-select .checkbox-btn').removeClass('active');
        $('#editCouponModal .room-type').find('.list .checkbox-btn').removeClass('active');
        formGroup.removeClass('has-error has-warning');
        ajaxRequest('/Home/BookSetting/hasCouponDetail', {
            'hotel':window.currentHotel,
            'couponId':couponId
        }, 'post', function (data) {
            var info = data.data;
            var couponUseMan = info.couponUseMan;
//          window.datearrAnew = info.couponDate;
            window.numberValue = info.couponNumber;
            window.weekarr = info.couponWeek;
            window.datearr = info.couponDate;
            editCouponModal.find('#couponNameEdit').val(info.couponName);
            editCouponModal.find('.coupon-number').text(info.couponNumber);
            editCouponModal.find('#startTimeEdit').val(info.couponStart);
            editCouponModal.find('#endTimeEdit').val(info.couponEnd);
            editCouponModal.find('#inputUseNumberEdit').val(info.couponReceiveTotal);
            $('#submitInfoEdit').data('couponid',couponId);
            if(parseInt(info.couponType)==1){
                editCouponModal.find('.coupon-type span').text(lang('Coupon.01'));
                editCouponModal.find('.request-money span').text(lang('Coupon.07')+info.couponPrice+lang('Coupon.08')+window.symbols+info.couponMinusPrice);
            }else{
                editCouponModal.find('.coupon-type span').text(lang('Coupon.02'));
                editCouponModal.find('.request-money span').text(lang('Coupon.07')+info.couponPrice+lang('Coupon.10')+info.couponDiscount+lang('Coupon.11'));
            }
            if(couponUseMan == 1){
                crowdList.find('.checkbox-btn').addClass('active');
            }else if(couponUseMan == 2){
                crowdList.eq(0).find('.checkbox-btn').addClass('active');
            }else{
                crowdList.eq(1).find('.checkbox-btn').addClass('active');
            }
            $.each(info.couponWeek,function(item,value){
                $('.week-select').find('.checkbox-btn[data-week='+value+']').addClass('active');
            });
            $.each(info.couponRoomTypeData,function(item,value){
                $('#editCouponModal .room-type').find('.list[data-value='+value.roomtype_id+'] .checkbox-btn').addClass('active');
            });
            var weekDay = '';
            $.each(info.couponWeek,function(item,value){
                weekDay+= '、'+weekdayArr[value];
            });
            var dateDay = '';
            $.each(info.couponDate,function(item,value){
                dateDay+= '、'+value;
            });
            if(weekDay.substring(1)!=''){
                weekDay = lang('Coupon.04') + weekDay.substring(1);
                if(dateDay.substring(1)!=''){
                    dateDay = '、' + dateDay.substring(1);
                }            
            }else{
                if(dateDay.substring(1)!=''){
                    dateDay = lang('Coupon.04') + dateDay.substring(1);
                }            
            }
            $('#editCouponModal .cannot-use').text(weekDay+dateDay);        
            getroomTypeEdit();
            
        }, function (error) {          
            if (error.responseJSON !== undefined) {
                modalAlert(error.responseJSON.info);
            }
        });           
    });      
    
    //使用人群
    $('#addCouponModal').on('click','.use-crowd .checkbox-btn',function(e){
        e.stopPropagation();
        var btn = $(this);
        if(btn.hasClass('active')){
            btn.removeClass('active');
        }else{
            btn.addClass('active');
        }
    });
    
    //劵面值切换
    $('#addCouponModal').on('click', '.addCouponMenu li>a', function(e) {
        var item = $(this);
        var value = item.parents('li').data('value');
        var textvalue = item.text();
        var subtract = $('#requestSubtractMoney');
        var discount = $('#requestDiscountMoney');
        item.parents('li').addClass('active').siblings().removeClass('active');
        item.parents('.dropdown').find('a[data-toggle=dropdown]').attr('data-value',value).find('.info').text(textvalue);
        if(value == 1){
            $('.text-value').text(lang('Coupon.08'));
            $('.text-value-last').text('');
            discount.addClass('hidden');
            subtract.removeClass('hidden');
        }else{
            $('.text-value').text(lang('Coupon.10'));
            $('.text-value-last').text(lang('Coupon.11'));
            subtract.addClass('hidden');
            discount.removeClass('hidden');
        }
    }); 
    //每人可用加减
    var inputUseNumber = $('#inputUseNumber');
    $('#addCouponModal .number-container').on('click','.reduce-btn',function(){
        var value = parseInt(inputUseNumber.val());
        if(value>1){
            value--;
        }
        inputUseNumber.val(value);
    });
    $('#addCouponModal .number-container').on('click','.add-btn',function(){
        var number =  $('#number').val();
        var value = parseInt(inputUseNumber.val());
        if(number!=='' && number!= 0){
            if(value<number){
                value++;
            }            
        }else{
            value++;
        }
        inputUseNumber.val(value);
    });
    var getinputUseNumber = function(){
        var number = $('#number').val();
        var value = parseInt(inputUseNumber.val());
        if(number!==''&& number!= 0){
            if(value>number){
                value = number
            }            
        }
        if(value<1){
            value = 1;
        }
        inputUseNumber.val(value);
    }
    inputUseNumber.blur(function(){
        getinputUseNumber();
    });
    $('#number').blur(function(){
        getinputUseNumber();
    });    
    
    //房型选择
    var roomType = $('#addCouponModal .room-type .list');
    $('#addCouponModal').on('click','.all-room-type',function(e){
        e.stopPropagation();
        var btn = $(this);
        if(btn.hasClass('active')){
            btn.removeClass('active');
            roomType.find('.checkbox-btn').removeClass('active');
        }else{
            btn.addClass('active');
            roomType.find('.checkbox-btn').addClass('active');
        }
    });    
    var getroomType = function(){
        var isAllSelect = true;
        roomType.each(function(){
            if(!$(this).find('.checkbox-btn').hasClass('active')){
                isAllSelect = false;
            }
        });
        if(isAllSelect){
            $('#addCouponModal .all-room-type').addClass('active');
        }else{
            $('#addCouponModal .all-room-type').removeClass('active');
        }
    }
    $('#addCouponModal').on('click','.room-type .checkbox-btn',function(e){
        e.stopPropagation();
        var btn = $(this);
        if(btn.hasClass('active')){
            btn.removeClass('active');
        }else{
            btn.addClass('active');
        }
        getroomType();
    }); 
    
    //修改房型选择
    var roomTypeEdit = $('#editCouponModal .room-type .list');
    $('#editCouponModal').on('click','.all-room-type',function(e){
        e.stopPropagation();
        var btn = $(this);
        if(btn.hasClass('active')){
            btn.removeClass('active');
            roomTypeEdit.find('.checkbox-btn').removeClass('active');
        }else{
            btn.addClass('active');
            roomTypeEdit.find('.checkbox-btn').addClass('active');
        }
    });    
    var getroomTypeEdit = function(){
        var isAllSelect = true;
        roomTypeEdit.each(function(){
            if(!$(this).find('.checkbox-btn').hasClass('active')){
                isAllSelect = false;
            }
        });
        if(isAllSelect){
            $('#editCouponModal .all-room-type').addClass('active');
        }else{
            $('#editCouponModal .all-room-type').removeClass('active');
        }
    }
    $('#editCouponModal').on('click','.room-type .checkbox-btn',function(e){
        e.stopPropagation();
        var btn = $(this);
        if(btn.hasClass('active')){
            btn.removeClass('active');
        }else{
            btn.addClass('active');
        }
        getroomTypeEdit();
    });      
    //修改使用人群
    $('#editCouponModal').on('click','.use-crowd .checkbox-btn',function(e){
        e.stopPropagation();
        var btn = $(this);
        if(btn.hasClass('active')){
            btn.removeClass('active');
        }else{
            btn.addClass('active');
        }
    });
    //修改每人可用加减
    var inputUseNumberEdit = $('#inputUseNumberEdit');
    $('#editCouponModal .number-container').on('click','.reduce-btn',function(){
        var value = parseInt(inputUseNumberEdit.val());
        if(value>1){
            value--;
        }
        inputUseNumberEdit.val(value);
    });
    $('#editCouponModal .number-container').on('click','.add-btn',function(){
        var value = parseInt(inputUseNumberEdit.val());
        if(window.numberValue != 0){
            if(value<window.numberValue){
                value++;
            }            
        }else{
            value++;
        }
        inputUseNumberEdit.val(value);
    });
    var getinputUseNumberEdit = function(){
        var value = parseInt(inputUseNumberEdit.val());
        if(window.numberValue!= 0){
            if(value>window.numberValue){
                value = window.numberValue;
            }            
        }
        if(value<1){
            value = 1;
        }
        inputUseNumberEdit.val(value);
    }
    inputUseNumberEdit.blur(function(){
        getinputUseNumberEdit();
    });
     
    //添加优惠券
    $('#doc-center-head-menu').on('click','.addCoupon',function(e) {
        window.weekarr = [];
        window.datearr = [];
        var formGroup = $('#addCouponModal .form-group');
        formGroup.removeClass('has-error has-warning');
        $('#calendar-panel .week-select .checkbox-btn').removeClass('active');
    });
    $('#submitInfo').on('click',function (e) {
        var submitBtn = $(this);
        var formGroup = $('#addCouponModal .form-group');
        var name = $('#couponName');
        var couponType = $('#couponType').attr('data-value');
        var requestMoney = $('#requestMoney').val();
        var requestSubtractMoney = $('#requestSubtractMoney').val();
        var requestDiscountMoney = $('#requestDiscountMoney').val();
        var usecrowd = 2;
        var minusPrice = 0;
        var discount = 0;
        var temp = 0;
        var roomTypeIds =[];
        var numberValue = $('#number').val();
        numberValue = numberValue !== '' ? numberValue : 0;
        formGroup.removeClass('has-error');
        if(name.val() == ''){
            formInlineTip(submitBtn, lang('Coupon.12'));
            return;
        }
        if(requestMoney == ''){
            formInlineTip(submitBtn, lang('Coupon.13'));
            return;
        }
        if(couponType == 1){
            if(requestSubtractMoney == ''){
                formInlineTip(submitBtn, lang('Coupon.14'));
                return;
            }    
            if(parseFloat(requestSubtractMoney) > parseFloat(requestMoney)){
                formInlineTip(submitBtn, lang('Coupon.20'));
                return;
            }    
            minusPrice = requestSubtractMoney;
        }else{
            if(requestDiscountMoney == ''){
                formInlineTip(submitBtn, lang('Coupon.15'));
                return;
            }    
            discount = requestDiscountMoney;
        }
        $('#addCouponModal .use-crowd .list').each(function(){
            if($(this).find('.checkbox-btn').hasClass('active')){
                usecrowd = $(this).data('crowd');
                temp++;
            }
        });
        if(temp == 0){
            formInlineTip(submitBtn, lang('Coupon.16'));
            return;
        }else if(temp == 2){
            usecrowd = 1;
        }
        roomType.each(function(){
            if($(this).find('.checkbox-btn').hasClass('active')){
                roomTypeIds.push($(this).data('value'));
            }
        });      
        if(roomTypeIds.length == 0){
            formInlineTip(submitBtn, lang('Coupon.17'));
            return;
        }
        var params = {};
        params.hotel = window.currentHotel;
        params.name = name.val();
        params.useMan = usecrowd;
        params.roomType = 1;
        params.couponType = couponType;
        params.price = requestMoney;
        params.minusPrice = minusPrice;
        params.discount = discount;
        params.number = numberValue;
        params.useNumber = inputUseNumber.val();
        params.start = $('#startTime').val();
        params.end = $('#endTime').val();
        params.week = window.weekarr;
        params.roomTypeIds = roomTypeIds;
        params.disableDates = window.datearr;
        ajaxRequest('/Home/BookSetting/setCoupon', params, 'post', function (data) {
            getHotelCoupon();
            formInlineTip(submitBtn, lang('successfullySave'), 2);
            $('#addCouponModal').modal('hide');
        }, function (error) {
            formInlineTip(submitBtn, error.responseJSON.info, 0);
        }, submitBtn);
    });
    

    //修改优惠券
    $('#submitInfoEdit').on('click',function (e) {
        var submitBtn = $(this);
        var formGroup = $('#editCouponModal .form-group');
        var name = $('#couponNameEdit');
        var usecrowd = 2;
        var temp = 0;
        var roomTypeIds =[];
        var numberValue = $('#number').val();
        numberValue = numberValue !== '' ? numberValue : 0;
        formGroup.removeClass('has-error');
        if(name.val() == ''){
            formInlineTip(submitBtn, lang('Coupon.12'));
            return;
        }
        $('#editCouponModal .use-crowd .list').each(function(){
            if($(this).find('.checkbox-btn').hasClass('active')){
                usecrowd = $(this).data('crowd');
                temp++;
            }
        });
        if(temp == 0){
            formInlineTip(submitBtn, lang('Coupon.16'));
            return;
        }else if(temp == 2){
            usecrowd = 1;
        }
        roomTypeEdit.each(function(){
            if($(this).find('.checkbox-btn').hasClass('active')){
                roomTypeIds.push($(this).data('value'));
            }
        });      
        if(roomTypeIds.length == 0){
            formInlineTip(submitBtn, lang('Coupon.17'));
            return;
        }
        var params = {};
        params.hotel = window.currentHotel;
        params.couponId = submitBtn.data('couponid');
        params.name = name.val();
        params.useMan = usecrowd;
        params.roomType = 1;
        params.useNumber = inputUseNumberEdit.val();
        params.start = $('#startTimeEdit').val();
        params.end = $('#endTimeEdit').val();
        params.week = window.weekarr;
        params.roomTypeIds = roomTypeIds;
        params.disableDates = window.datearr;
        ajaxRequest('/Home/BookSetting/editCoupon', params, 'post', function (data) {
            getHotelCoupon();
            formInlineTip(submitBtn, lang('successfullySave'), 2);
            $('#editCouponModal').modal('hide');
        }, function (error) {
            formInlineTip(submitBtn, error.responseJSON.info, 0);
        }, submitBtn);
    });

    
    //设置不可用日期
    $('#editCouponModal').on('click','.set-cannot-time',function(e){
        e.preventDefault();
        var startTime = $('#startTimeEdit').val();
        var endTime = $('#endTimeEdit').val();
        window.date = getDate(startTime);
        window.DateEnd = getDate(endTime);
//      window.datearr = window.datearrAnew;
        window.startTime = startTime;
        window.endTime = endTime;
        refreshAllCalendar();
        $('#calendarModal').modal('show');
        $(".modal-backdrop").eq(1).remove();
    });    
    $('#addCouponModal').on('click','.set-cannot-time',function(e){
        e.preventDefault();
        var startTime = $('#startTime').val();
        var endTime = $('#endTime').val();
        window.date = getDate(startTime);
        window.DateEnd = getDate(endTime);
//      window.datearr = window.datearrAnew;
        window.startTime = startTime;
        window.endTime = endTime;
        refreshAllCalendar();
        $('#calendarModal').modal('show');
        $(".modal-backdrop").eq(1).remove();
    });
    $('#calendarSubmitInfo').on('click',function(e){
        e.preventDefault();
        window.weekarr=[];
        $('.week-select .flex-1').each(function(){
            var btn = $(this).find('.checkbox-btn');
            var week = btn.data('week');
            if(btn.hasClass('active')){
                 window.weekarr.push(week);
            }        
        });
        var weekDay = '';
        $.each(window.weekarr,function(item,value){
            weekDay+= '、'+weekdayArr[value];
        });
        var dateDay = '';
        $.each(window.datearr,function(item,value){
            dateDay+= '、'+value;
        });

        if(weekDay.substring(1)!=''){
            weekDay = lang('Coupon.04') + weekDay.substring(1);
            if(dateDay.substring(1)!=''){
                dateDay = '、' + dateDay.substring(1);
            }            
        }else{
            if(dateDay.substring(1)!=''){
                dateDay = lang('Coupon.04') + dateDay.substring(1);
            }            
        }
        $('.cannot-use').text(weekDay+dateDay);
        $('#calendarModal').modal('hide');
//      window.datearrAnew = window.datearr;
    });
    $('#calendarModal').on('click','.calendar-select',function(e){
        e.preventDefault();
        var btn = $(this);
        if(!btn.hasClass('week')){
            if(btn.hasClass('select-date')){
                btn.removeClass('select-date');
                window.datearr.splice($.inArray(btn.data('value'),window.datearr ),1);
            }else{
                btn.addClass('select-date');
                getDatasArr();
            }            
        }
    });
    $('#calendarModal').on('click','.checkbox-btn',function(e){
        e.stopPropagation();
        var btn = $(this);
        if(btn.hasClass('active')){
            btn.removeClass('active');
        }else{
            btn.addClass('active');
        }
        getcheckboxweek();
    });
    var getcheckboxweek = function(){
//      window.datearr = window.datearrAnew;
        $('.week-select .flex-1').each(function(){
            var btn = $(this).find('.checkbox-btn');
            var week = btn.data('week');
            if(btn.hasClass('active')){
                $('#calendar').find('.calendar-day[data-week='+week+']').addClass('select-date week');
            }else if($('#calendar').find('.calendar-day[data-week='+week+']').hasClass('week')){
                $('#calendar').find('.calendar-day[data-week='+week+']').removeClass('select-date week');
            }            
        });
        getDatasArr();
    };
    var getDatasArr = function(){
        $('#calendar .calendar-day').each(function(){
            var btn = $(this);
            var value = btn.data('value');
            if(!btn.hasClass('week')){
                if(btn.hasClass('select-date')){
                    window.datearr.push(value);
                }
            }else{
                if(btn.hasClass('select-date')){
                    if($.inArray(value, window.datearr)>=0){
                        window.datearr.splice($.inArray(value,window.datearr ),1);
                    }
                }                
            }
        });
        window.datearr = $.unique(window.datearr).sort();
    };
    var getDatasArrShow = function(){
        $.each(window.datearr,function(index,data){
            $('#calendar .calendar-day').each(function(){
                var btn = $(this);
                var value = btn.data('value');
                if(value == data){
                    if(!btn.hasClass('week')){                    
                        btn.addClass('select-date');
                    }
                }
            });
        });        
    };    
    var weekday = [lang('week.7'),lang('week.1'),lang('week.2'),lang('week.3'),lang('week.4'),lang('week.5'),lang('week.6')];
    //获得某月第一天是周几
    function firstDay(day){
        return day.getDay();
    }    
    var refreshCalendar = function(startDate,week,days) {
        // 计算节假日填充calendar
        var calendarItems = '';
        window.istoday = false;
        $('#calendar-head .last-month').removeClass('disabled-time');
        $('#calendar-head .next-month').removeClass('disabled-time');
        if(parseInt(week)>0){
            for (var i = week; i > 0; i--) {
                calendarItems += createCalendarItem(startDate.customDays(-i),false);
            }                
        }
        for (var i = 0; i < 42-week; i++) {
            if(i<days){
                calendarItems += createCalendarItem(startDate.customDays(i),true);
            }else{
                calendarItems += createCalendarItem(startDate.customDays(i),false);
            }
        }
        calendarItems += '<span style="width: 30px;min-width: 30px;"></span>';
        $('#calendar').html(calendarItems);
        $('#calendar').find('.calendar-day[data-value='+window.startTime+']').prevAll('.calendar-day').addClass('disabled-time').removeClass('calendar-select');
        $('#calendar').find('.calendar-day[data-value='+window.endTime+']').nextAll('.calendar-day').addClass('disabled-time').removeClass('calendar-select');
        getcheckboxweek();
        getDatasArrShow();
    };
    var templateCalendarDay = $('#templateCalendarDay');
    var createCalendarItem = function(date,type) {
        var template = $(templateCalendarDay.clone().html());
        var dayText = '';
        if (date.customFormat() === new Date().customFormat()) {
            dayText = lang('today');
            template.addClass('today');
        } else {
            dayText = date.customFormat('M/d');
        }
        template.attr('data-value', date.customFormat());
        template.attr('data-week', firstDay(date));
        template.find('.day-text').html(dayText);
        var weekText = getLunarFestivalOrTerm(date);
        if ($.inArray(weekText, weekday) === -1 || weekText == lang('week.7') || weekText == lang('week.6')) {
            template.addClass('has-festival');
        }
        template.find('.week-text').html(weekText);
        if(window.week>=7 && $.inArray(weekText, weekday) !== -1 ){
            template.find('.week-text').addClass('hidden');
        }else{
            window.week++;
        }
        if(type){  
            template.removeClass('disabled-time');
            template.addClass('calendar-select');
        }else{
            template.addClass('disabled-time');
            template.removeClass('calendar-select');
        }
        if (date.customFormat() === window.startTime) {
            $('#calendar-head .last-month').addClass('disabled-time');
        }
        if (date.customFormat() === window.endTime) {
            if(type){
                $('#calendar-head .next-month').addClass('disabled-time');
            }else{
                $('#calendar-head .next-month').removeClass('disabled-time');
            }
        }
        return template.prop('outerHTML');
    };
    /**
     * 获取节假日或二十四节气，其余情况获取周几
     */
    var getLunarFestivalOrTerm = function(date) {
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var info = LunarCalendar.solarToLunar(year, month, day);
        if (info.lunarFestival !== undefined) {
            return info.lunarFestival;
        }
        if (info.solarFestival !== undefined) {
            return info.solarFestival;
        }
        return weekday[date.getDay()];
    };    
    //获得当天的相关日期变量
    function dateNoneParam(){
        var day = window.date;
        var today = new Array();
        today['year'] = day.getFullYear();
        today['month'] = day.getMonth();
        today['date'] = day.getDate();
        today['week'] = day.getDay();
        today['firstDay'] = new Date(today['year'],today['month'],1); 
        return today;
    }   
    function leapYear(year,month){
        var isLeap = year%100==0 ? (year%400==0 ? 1 : 0) : (year%4==0 ? 1 : 0);
        var days = new Array(31,28+isLeap,31,30,31,30,31,31,30,31,30,31);
        return days[month]
    }  
    var nowDay = $('#calendar-head .now-day');
    refreshAllCalendar = function(){
        window.week = 0;
        var year = dateNoneParam().year;
        var month = dateNoneParam().month;
        var days = leapYear(year,month);
        var week = firstDay(dateNoneParam().firstDay)-1;
        if(week<0){
           week = 6; 
        }
        nowDay.text(year+lang('years01')+parseInt(month+1)+lang('months01')).data({'year':year,'month':parseInt(month+1)})
        refreshCalendar(dateNoneParam().firstDay,week,days);
    }
    $('#calendar-head').on('click','.last-month',function(e){
        e.preventDefault();
        var month = parseInt(nowDay.data('month'));
        var year = parseInt(nowDay.data('year'));
        if(month==1){
            month = 12;
            year--;
        }else{
            month--;
        }
        var date = year +'-'+month+'-01';
        window.date = getDate(date);
        refreshAllCalendar();
    });
    $('#calendar-head').on('click','.next-month',function(e){
        e.preventDefault();
        var month = parseInt(nowDay.data('month'));
        var year = parseInt(nowDay.data('year'));
        if(month==12){
            month = 1;
            year++;
        }else{
            month++;
        }
        var date = year +'-'+month+'-01';
        window.date = getDate(date);
        refreshAllCalendar();
    });
});
